package com.woniuxy.util;
/*
 * @author 幸琦林
 *
 */

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Random;


public class SendMessage {

    public static void main(String[] args) {
        getRandomCode();
    }

    public static void sendSms(String phoneNum, String CodeNum) {
//      这里去阿里申请密钥
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4Fz8BX88MTPTs97kejBQ", "z0M4LasScwKd7UjWysNMQI9aB4h1i5");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
//        必填参数，手机号，签名，生成的验证码
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "hifit");
        request.putQueryParameter("TemplateCode", "SMS_193180046");
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + CodeNum + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public static String getRandomCode() {
        int flag = new Random().nextInt(999999);
        if (flag < 100000) {
            flag += 100000;
        }
        return flag + "";
    }
}
